﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using System.Linq.Expressions;

namespace Northwinddemo.Web.Controllers
{
    public abstract class ControllerEx : Controller
    {
        ILog log;
        
        protected ControllerEx()
        {
            log = log4net.LogManager.GetLogger(typeof(ControllerEx));
        }
        
        protected override void OnException(ExceptionContext filterContext)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(filterContext.Exception.Message, filterContext.Exception);
            }
            filterContext.ExceptionHandled = true;
            if (Request.IsAjaxRequest())
            {
                filterContext.Result = JavaScript("Error"); // todo: display some reasonable javascript error somehow JavaScript("window.location=\"" + Url.Content("~/Error.aspx") + "\";");
                return;
            }
            filterContext.Result = View("Error", filterContext.Exception);
        }

    }
}
